/// <summary>
/// 添加转发组按钮点击事件处理
/// </summary>
private async void OnAddForwardGroup(object sender, RoutedEventArgs e)
{
    btnAdd.IsEnabled = false;
    try
    {
        if (!int.TryParse(txtPort1.Text, out int port1) || port1 <= 0 || port1 > 65535)
        {
            MessageBox.Show("请输入有效的监听端口号（1-65535）", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }

        if (!int.TryParse(txtPort2.Text, out int port2) || port2 <= 0 || port2 > 65535)
        {
            MessageBox.Show("请输入有效的转发端口号（1-65535）", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }

        // 获取选择的IP地址
        IPAddress selectedIP = IPAddress.Any;
        if (cboListenIP.SelectedItem != null && cboListenIP.SelectedIndex > 0) // 直接获取选中项
        {
            // 确保SelectedItem的类型正确
            object selectedItem = cboListenIP.SelectedItem;
            string selectedItemText = selectedItem is ComboBoxItem comboBoxItem 
                ? comboBoxItem.Content?.ToString() 
                : selectedItem?.ToString();

            if (!string.IsNullOrEmpty(selectedItemText) && !IPAddress.TryParse(selectedItemText, out selectedIP))
            {
                MessageBox.Show("无效的IP地址", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }

        // 保存当前输入值，避免在后台线程中访问UI控件
        string remark = txtRemark.Text;

        // 检查端口是否可用
        bool canStart = await Task.Run(() =>
        {
            if (!ForwardGroup.IsPortAvailable(port1))
            {
                MessageBox.Show($"监听端口 {port1} 已被占用", "错误",
                    MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            if (!ForwardGroup.IsPortAvailable(port2))
            {
                MessageBox.Show($"转发端口 {port2} 已被占用", "错误",
                    MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            return true;
        });

        if (!canStart)
        {
            return;
        }

        // 创建新的转发组
        var newGroup = new ForwardGroup(nextId++, port1, port2, remark);
        newGroup.UpdateListenIP(selectedIP);

        // 启动转发服务
        try
        {
            // 先创建UI控件
            var groupControl = CreateForwardGroupControl(newGroup);

            // 启动服务
            await Task.Run(() => newGroup.Start());

            // 在UI线程中更新界面
            await Dispatcher.InvokeAsync(() =>
            {
                try
                {
                    // 添加到列表
                    ForwardGroups.Add(newGroup);

                    // 添加UI控件
                    forwardGroupsPanel.Children.Add(groupControl);

                    // 清空输入框
                    txtPort1.Clear();
                    txtPort2.Clear();
                    txtRemark.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"更新界面时出错: {ex.Message}", "错误",
                        MessageBoxButton.OK, MessageBoxImage.Error);
                }
            });
        }
        catch (Exception ex)
        {
            MessageBox.Show($"启动转发服务失败：{ex.Message}", "错误",
                MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
    finally
    {
        btnAdd.IsEnabled = true;
    }
}
